package com.example.apiproject.controller;

import com.example.apiproject.entity.Information;
import com.example.apiproject.service.InformationService;
import com.example.apiproject.utils.R;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Information)表控制层
 *
 * @author makejava
 * @since 2024-04-03 23:03:28
 */
@RestController
@RequestMapping("information")
public class InformationController {
    /**
     * 服务对象
     */
    @Resource
    private InformationService informationService;

    /**
     * 分页查询
     *
     * @param information 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Information>> queryByPage(Information information, PageRequest pageRequest) {
        return ResponseEntity.ok(this.informationService.queryByPage(information, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Information> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.informationService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param information 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public R add(@RequestBody Information information) {
        if(this.informationService.insert(information)==1)
        {return R.ok();}
        return R.error("提交失败");
    }

    /**
     * 编辑数据
     *
     * @param information 实体
     * @return 编辑结果
     */
    @PutMapping("/update")
    public ResponseEntity<Information> edit(Information information) {
        return ResponseEntity.ok(this.informationService.update(information));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.informationService.deleteById(id));
    }

}

